<?php

class Images
{
	private $tmp;
	private $dir_path;

	function __construct($dir='images')//构造方法用来对图片所在位置进行初使化
	{
		$this->dir_path = ROOT.'/'.$dir.'/'.date('Ymd');
	}

	function upLoad($name, $size=array('width'=>800,'height'=>800), $water=false)//图片上传
	{
		$file = $_FILES[$name];
		$this->tmp = $file['tmp_name'];

		if($file['error'])
		{
			notice('上传文件错误...', 'test.html');
		}		
		if(!file_exists($this->dir_path))
		{
			mkdir($this->dir_path, 0777, true);
		}

		$imgName = '/b_'.time().'_'.$size['width'].'_'.$size['height'];
		$result = $this->thumb($size, $imgName);
		if($water === true)
		{
			$waterName = 'water.jpg';
			$result = $this->waterMark($imgName, $waterName, $waterPos=0, $imgName);
		}
		return $result;	
	}

	/* 对图片进行缩放
	*
	* 参数$name: 是需要处理的图片名称
	* 参数$width:是缩放后的宽度
	* 参数$height:是缩放后的高度
	* 参数$qz: 是新图片的名称前缀
	* 返回值:就是缩放后的图片名称，失败则返回false
	*
	*/
	function thumb($size, $imgName="th_")
	{	
		$size = $this->getNewSize($size);//获取计算图片等比例之后的大小, $size["width"], $size["height"]

		$newImg = $this->kidOfImage($size);//获取新的图片资源, 处理一下gif透明背景
		
		return $this->createNewImage($newImg, $imgName); //另存为一个新的图片，返回新的缩放后的图片名称 
	}

	private function createNewImage($newImg, $imgName)
	{
		$imgInfo = $this->getInfo($this->tmp);
		switch($imgInfo["type"])
		{
			case 1://gif
				imageGif($newImg, $this->dir_path.$imgName.'.gif');
				$result = $this->dir_path.$imgName.'.gif';
			break;
			case 2://jpg
				imageJPEG($newImg, $this->dir_path.$imgName.'.jpg');
				$result = $this->dir_path.$imgName.'.jpg';
			break;
			case 3://png
				imagepng($newImg, $this->dir_path.$imgName.'.png');
				$result = $this->dir_path.$imgName.'.png';
			break;
			default:
				$result = false;
			break;
		}
		imagedestroy($newImg);
		return $result;
	}

	private function kidOfImage($size)
	{
		$srcImg = $this->getImg($this->tmp);//获取图片资源, 各种类型的图片都可以创建资源 jpg, gif, png
		$imgInfo = $this->getInfo($this->tmp);
		$newImg = imagecreatetruecolor($size["width"], $size["height"]);

		$otsc = imagecolortransparent($srcImg);

		if($otsc >=0 && $otsc <= imagecolorstotal($srcImg))
		{
			$tran=imagecolorsforindex($srcImg, $otsc);

			$newt=imagecolorallocate($newImg, $tran["red"], $tran["green"], $tran["blue"]);

			imagefill($newImg, 0, 0, $newt);

			imagecolortransparent($newImg, $newt);
		}

		ImageCopyResampled($newImg, $srcImg, 0, 0, 0, 0, $size["width"], $size["height"], $imgInfo["width"], $imgInfo["height"]);

		imagedestroy($srcImg);

		return $newImg;
	}

	private function getNewSize($size)
	{
		$imgInfo = $this->getInfo($this->tmp);

		$w_Per = $size['width']/$imgInfo["width"];
		$h_Per = $size["height"]/$imgInfo["height"];
		if($w_Per > $h_Per)
		{
			$size['width'] = $h_Per*$imgInfo["width"];
		}else{
			$size['height'] = $w_Per*$imgInfo["height"];
		}

		return $size;
	}

	private function getInfo($dir)
	{
		$data = getImageSize($dir);
		$imageInfo = array(
						'width' => $data[0],
						'height' => $data[1],
						'type' => $data[2]
					 );
		return $imageInfo;
	}

	private function getImg($dir)
	{
		$imgInfo = $this->getInfo($dir);
		switch($imgInfo["type"]){
			case 1: //gif
				$img=imagecreatefromgif($dir);
			break;
			case 2: //jpg
				$img=imageCreatefromjpeg($dir);
			break;
			case 3: //png
				$img=imageCreatefrompng($dir);
			break;
			default:
			return false;
		}
		return $img;
	}

	/* 功能：为图片加水印图片
	* 参数$groundName: 背景图片，即需要加水印的图片
	* 参数$waterName: 水钱图片
	* 参数#aterPost：水印位置， 10种状态，
	*  0为随机位置
	*
	*  1. 为顶端居左  2. 为顶端居中  3 为顶端居右
	*  4  为中部居左  5. 为中部居中  6 为中部居右
	*  7 . 为底端居左 8. 为底端居中， 9. 为底端居右
	*
	* 参数$qz : 是加水印后的图片名称前缀
	* 返回值：就是处理后图片的名称
	*
	*/
	function waterMark($groundName, $waterName, $waterPos=0)
	{
		$dir = ROOT.'/'.$waterName;
		$src_img = $this->dir_path.$groundName.'.jpg';

		if(file_exists($src_img) && file_exists($dir))
		{
			$groundInfo = $this->getInfo($src_img);
			$waterInfo = $this->getInfo($dir);
			
			if(!$pos = $this->position($groundInfo, $waterInfo, $waterPos))//水印的位置
			{
				echo "水印不应该比背景图片小！";
				return;
			}

			$groundImg = $this->getImg($src_img);
			$waterImg = $this->getImg($dir);
			$groundImg = $this->copyImage($groundImg, $waterImg, $pos, $waterInfo);

			return $this->createNewImage($groundImg, $groundName);
		}else{
			echo "图片或水印图片不存在";
			return false;
		}
	}

	private function copyImage($groundImg, $waterImg, $pos, $waterInfo)
	{
		imagecopy($groundImg, $waterImg, $pos["posX"], $pos["posY"], 0, 0, $waterInfo["width"], $waterInfo["height"]);
		imagedestroy($waterImg);
		return $groundImg;
	}

	private function position($groundInfo, $waterInfo, $waterPos)
	{		
		if(($groundInfo["width"]< $waterInfo["width"]) ||($groundInfo["height"] < $waterInfo["height"]))//需要背景比水印图片大
		{
			return false;
		}
		switch($waterPos)
		{
			case 1:
				$posX=0;
				$posY=0;
			break;
			case 2:
				$posX=($groundInfo["width"]-$waterInfo["width"])/2;
				$posY=0;
			break;
			case 3:
				$posX=$groundInfo["width"]-$waterInfo["width"];
				$posY=0;
			break;
			case 4:
				$posX=0;
				$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
			break;
			case 5:
				$posX=($groundInfo["width"]-$waterInfo["width"])/2;
				$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
			break;
			case 6:
				$posX=$groundInfo["width"]-$waterInfo["width"];
				$posY=($groundInfo["height"]-$waterInfo["height"]) /2;
			break;
			case 7:
				$posX=0;
				$posY=$groundInfo["height"]-$waterInfo["height"];
			break;
			case 8:
				$posX=($groundInfo["width"]-$waterInfo["width"])/2;
				$posY=$groundInfo["height"]-$waterInfo["height"];
			break;
			case 9:
				$posX=$groundInfo["width"]-$waterInfo["width"];
				$posY=$groundInfo["height"]-$waterInfo["height"];
			break;
			case 0:
			default:
				$posX=rand(0, ($groundInfo["width"]-$waterInfo["width"]));
				$posY=rand(0, ($groundInfo["height"]-$waterInfo["height"]));
			break;
		}
		return array("posX"=>$posX, "posY"=>$posY);
	}

	/*
	$scale 宽高比例
	*/
	public function imgview($imgurl,$scale)
	{
		header("Content-type: image/jpeg");
		$im = $this->getImg($imgurl);		
		list($width, $height) = getimagesize($imgurl);
		$ofs = $width/$height;
		if($ofs > $scale)//太宽
		{
			$newwidth = $height*$scale;
			$newheight = $height;
			$wof = ($width-$newwidth)*0.5;
			$hof = 0;
		}else{
			$newwidth = $width;
			$newheight = $width/$scale;
			$wof = 0;
			$hof = ($height-$newheight)*0.5;
		}
		$thumb = imagecreatetruecolor($newwidth,$newheight);
		imagecopyresized($thumb, $im,0,0,$wof,$hof, $newwidth, $newheight,$newwidth, $newheight);
		imagejpeg($thumb);
		imagedestroy($thumb);
		exit;
	}

}